#!/bin/bash

# 此脚本需要在消息队列节点执行,包括检测进程状态、端口状态、实例状态、服务队列长度、通道数等

# 系统ID
system_id=$(cat config.cnf | grep SystemId | awk -F "=" '{print $2}')
# 告警存放文件夹
alert_dir=$(cat config.cnf | grep AlertDir | awk -F "=" '{print $2}')
# RabbitMQ信息
rabbitmq_port=$(cat config.cnf | grep RabbitMQPort | awk -F "=" '{print $2}')
rabbitmq_user=$(cat config.cnf | grep RabbitMQUser | awk -F "=" '{print $2}')
rabbitmq_pass=$(cat config.cnf | grep RabbitMQPass | awk -F "=" '{print $2}')
rabbitmq_queue_len=$(cat config.cnf | grep RabbitMQQueueLen | awk -F "=" '{print $2}')
rabbitmq_channel_len=$(cat config.cnf | grep RabbitMQChannelLen | awk -F "=" '{print $2}')
# 告警缓存文件
alarm_file_tmp="tmp/rabbitmq_check_alarm.txt"
echo -n "" >${alarm_file_tmp}
# 本机ip
local_ip=$(ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:")
execute_cycle=$(cat config.cnf | grep ExecuteCycle | awk -F "=" '{print $2}')

# rabbitmq进程状态监控
rabbitmq_pro_check() {
    rabbitmq_pro=$(ps -elf | grep rabbit | grep -v grep | grep -v cmprabbitmq |grep -v rabbitmq_check | wc -l)
    if [[ ${rabbitmq_pro} == 0 ]]; then
        # 进程状态不存在,告警
        ./gen_alarm.sh -g "应用进程监控" "RabbitMQ进程不存在,IP:$(hostname -I)" 3 "RabbitMQ进程" "rabbitmq_pro" ${alarm_file_tmp}
    else
        # 进程存在且告警存在,则恢复原告警
        ./gen_alarm.sh -r "应用进程监控" "RabbitMQ进程恢复正常,IP:$(hostname -I)" "RabbitMQ进程" "rabbitmq_pro" ${alarm_file_tmp}
		
		# 端口检查
        rabbitmq_port_check
        # 实例状态检查
        rabbitmq_node_check
        # 服务队列长度
        rabbitmq_queue_len_check
		# 服务队列状态
		rabbitmq_queue_state_check
        # 通道数等
        rabbitmq_channel_len_check
    fi
}

# rabbitmq运行端口监控
rabbitmq_port_check() {
    port=$(netstat -nplt | grep ":${rabbitmq_port} ")
    if [[ ${port} == '' ]]; then
        # 端口未监听,告警
         ./gen_alarm.sh -g "端口状态" "RabbitMQ服务端口[${rabbitmq_port}]未监听,IP:$(hostname -I)" 3 "RabbitMQ端口状态" "rabbitmq_port" ${alarm_file_tmp}
    else
        # 端口正常且存在告警,需要恢复告警
        ./gen_alarm.sh -r "端口状态" "RabbitMQ服务端口[${rabbitmq_port}]恢复正常,IP:$(hostname -I)" "RabbitMQ端口状态" "rabbitmq_port" ${alarm_file_tmp}
    fi
}

# 实例状态
rabbitmq_node_check() {
    node_status=$(curl -u ${rabbitmq_user}:${rabbitmq_pass} http://localhost:15672/api/healthchecks/node | awk -F ":" '{print $2}' | sed 's/"//g' | sed 's/}//g')
    if [[ ${node_status} != 'ok' ]]; then
        # 实例状态异常进行告警
        ./gen_alarm.sh -g "实例连通性" "RabbitMQ实例状态异常,当前实例状态[${node_status}],IP:$(hostname -I)" 3 "RabbitMQ实例状态" "rabbitmq_node" ${alarm_file_tmp}
    else
        # 恢复告警
        ./gen_alarm.sh -r "实例连通性" "RabbitMQ实例状态恢复正常,当前实例状态[${node_status}],IP:$(hostname -I)" "RabbitMQ实例状态" "rabbitmq_node" ${alarm_file_tmp}
    fi
}

# 服务队列长度
rabbitmq_queue_len_check() {
    queue_len=$(curl -u ${rabbitmq_user}:${rabbitmq_pass} http://localhost:15672/api/overview | sed 's/,/\n/g' | grep '"messages":' | awk -F ":" '{print $3}')
    if [[ ${queue_len} -gt ${rabbitmq_queue_len} ]]; then
        # 实例状态异常进行告警
        ./gen_alarm.sh -g "服务队列" "RabbitMQ队列长度超出阈值${rabbitmq_queue_len},当前队列长度${queue_len},IP:$(hostname -I)" 2 "RabbitMQ队列长度" "rabbitmq_queue_len" ${alarm_file_tmp}
    else
        # 恢复告警
        ./gen_alarm.sh -r "服务队列" "RabbitMQ队列长度恢复正常,阈值${rabbitmq_queue_len},当前${queue_len},IP:$(hostname -I)" "RabbitMQ队列长度" "rabbitmq_queue_len" ${alarm_file_tmp}
    fi
}

# 服务队列状态
rabbitmq_queue_state_check() {
    queues=$(curl -u ${rabbitmq_user}:${rabbitmq_pass} http://localhost:15672/api/queues | sed 's/,/\n/g' | grep -E "name|state" | xargs -n 2 | awk '{print $1, $2}')
	echo $queues | while read line; do
		rabbitmq_queue_name=$(echo $line | awk -F : '{print $2}' | awk '{print $1}')	
		rabbitmq_queue_state=$(echo $line | awk -F : '{print $3}')
		if [[ ${rabbitmq_queue_state} != 'running' ]]; then
			# 服务队列状态异常进行告警
			./gen_alarm.sh -g "服务队列" "RabbitMQ队列状态异常,队列名称${rabbitmq_queue_name},当前队列状态${rabbitmq_queue_state},IP:$(hostname -I)" 2 "RabbitMQ队列状态" "rabbitmq_queue_state" ${alarm_file_tmp}
		else
			# 恢复告警
			./gen_alarm.sh -r "服务队列" "RabbitMQ队列状态恢复正常,队列名称${rabbitmq_queue_name},当前队列状态${rabbitmq_queue_state},IP:$(hostname -I)" "RabbitMQ队列状态" "rabbitmq_queue_state" ${alarm_file_tmp}
		fi
	done
}
 
# 通道数等
rabbitmq_channel_len_check() {
    channel_len=$(curl -u ${rabbitmq_user}:${rabbitmq_pass} http://localhost:15672/api/overview | sed 's/,/\n/g' | grep '"channels":' | awk -F ":" '{print $3}')
    if [[ ${channel_len} -gt ${rabbitmq_channel_len} ]]; then
        # 实例状态异常进行告警
         ./gen_alarm.sh -g "服务队列" "RabbitMQ通道数超出阈值${rabbitmq_channel_len},当前通道数${channel_len},IP:$(hostname -I)" 2 "RabbitMQ通道数" "rabbitmq_channel_len" ${alarm_file_tmp}
    else
        # 恢复告警
        ./gen_alarm.sh -r "服务队列" "RabbitMQ通道数恢复正常,阈值${rabbitmq_channel_len},当前通道数${channel_len},IP:$(hostname -I)" "RabbitMQ通道数" "rabbitmq_channel_len" ${alarm_file_tmp}
    fi
}

while true; do
    holidays_date=$(cat config.cnf | grep Holidays | awk -F "=" '{print $2}' | sed "s/,/ /g")
	current_date=$(date +'%Y-%m-%d')
	echo $holidays_date | grep -o $current_date >> /dev/null
	if [[ $? -eq 0 ]]; then
		exit 0
	else
		echo -n "" >${alarm_file_tmp}
		rabbitmq_pro_check
		
		# 将缓存告警放入告警文件夹
		./gen_alarm.sh -s ${alarm_file_tmp}
		sleep ${execute_cycle}
	fi
done